package com.huifaapp.mall.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.SharewxExplosionItemListviewAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.ShopList;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

/**
 *爆款分享
 * Created by Administrator on 2017/12/14.
 */

public class DetonationModelSharingFragment extends Fragment {
    private PullToRefreshListView lv_home_Detonation_model_share;
    ArrayList<String> list=new ArrayList<String>();
    List<ShopList.DataBean.ListBean> listb;
    SharewxExplosionItemListviewAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View viewLayout = inflater.inflate(R.layout.home_fragment_detonation_model_sharing, container, false);
        lv_home_Detonation_model_share= (PullToRefreshListView) viewLayout.findViewById(R.id.lv_home_Detonation_model_share);
        lv_home_Detonation_model_share.setMode(PullToRefreshBase.Mode.BOTH);//两端刷新
        lv_home_Detonation_model_share.setSelected(true);
        lv_home_Detonation_model_share.setScrollingWhileRefreshingEnabled(true);
        LoadStoreList(0, 0);
        lv_home_Detonation_model_share.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                LoadStoreList(0, 0);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                LoadStoreList(1, listb.size());


            }
        });

        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                lv_home_Detonation_model_share.setRefreshing(true);
                return false;
            }
        }).sendEmptyMessageDelayed(0, 200);

        return viewLayout;
    }
    int length = 5;
    int start = 0;

    private void LoadStoreList(final int downorup, int starts) {


        if (downorup == 0) {
            length = 5;
            start = 0;
        } else if (downorup == 1) {
            length = 5;
            start = starts;
        }
        String url = ApiManager.EVERYDAY_LIST+ "?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+ "&start=" + start + "&length=" + length+"&type=weixin";
        ;
        Log.e("WEB", "路径--" + url);
        HttpUtils http = new HttpUtils();
        //http://www.jhjvip.cn:8081/market/mobile/jd/index?
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//              testTextView.setText(current + "/" + total);
                Log.e("数据", "数据-------" + current + "/" + total);
            }

            //
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
//                    textView.setText(responseInfo.result);
                lv_home_Detonation_model_share.onRefreshComplete();
                Log.e("数据123", "数据d-------" + responseInfo.result);
                try {
                    Gson gson = new Gson();
                    ShopList list = gson.fromJson(responseInfo.result, ShopList.class);
                    if (list.getCode().equals("10000")) {
                        if (list.getData().getList() != null && list.getData().getList().size() > 0) {
                            lv_home_Detonation_model_share.setVisibility(View.VISIBLE);
                        } else {
                            if (start == 0) {
                                Log.e("数据", "首页数据---------" + list.getData().getList());
                                lv_home_Detonation_model_share.setVisibility(View.GONE);
                            } else {
                                lv_home_Detonation_model_share.setVisibility(View.VISIBLE);
                            }
                        }
                        if (list.getData().getList() != null && list.getData().getList().size() > 0) {


                            if (downorup == 0) {
                                ArrayList<Boolean> mCheckeds = new ArrayList<Boolean>();
                                for(int i=0;i<list.getData().getList().size();i++){
                                    mCheckeds.add(false);
                                }

                                listb = list.getData().getList();
                                adapter=new SharewxExplosionItemListviewAdapter(getActivity(),listb);
                                lv_home_Detonation_model_share.setAdapter(adapter);


                            } else {
                                listb.addAll(list.getData().getList());
                                if (listb.size() == 0) {
                                    lv_home_Detonation_model_share.setVisibility(View.GONE);
                                }
                                adapter.notifyDataSetChanged();
                                // listView.getRefreshableView().setSelection(a);

                            }

//                            mChecked = new ArrayList<Boolean>();
//                            for(int i=0;i<listb.size();i++){
//                                mChecked.add(false);
//                            }
                        }
                    } else {
                        Toast.makeText(getActivity(), list.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JsonSyntaxException e) {
                    Toast.makeText(getActivity(), "未知错误", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                lv_home_Detonation_model_share.onRefreshComplete();
            }
        });
    }


}
